package util{
	import editor.WorkflowexApp;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import model.xpdl2.Package;
	
	import mx.rpc.http.HTTPService;
	
	/*
	  对选择的流程进行装载, 并进行绘制.
	*/
	
	public class ProcessLoader{
		
		private var config:ConfigUtil;
		
		private var httpService:HTTPService;
		
		public function ProcessLoader(config:ConfigUtil){
			this.config=config;
		}
		
		public function loader(){
			var consoleUrl:String=config.getConsoleURL();
			
			var url=consoleUrl.charAt(consoleUrl.length-1)=='/'?consoleUrl:consoleUrl+"/";
			
			url=url+"upload/"+config.getPackagePath()+"/xpdl/"+config.getProcessName()+".xpdl2";
			
			trace("Process Path: "+url);
			
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, handleXMLLoaderComplete);
			loader.load(new URLRequest(url));
			/* var params:URLVariables = new URLVariables();                
            params.procDefId = httpEncoding(config.getProcessId());
			httpService.send(params);
			//httpService.method=HTTPRequestMessage.POST_METHOD;
			//httpService.resultFormat="xml";
     		httpService.addEventListener(ResultEvent.RESULT,resultHandler);
    		httpService.addEventListener(FaultEvent.FAULT,faultHandler); */
		}
		
		 public function handleXMLLoaderComplete(event:Event):void{
          	 
          	var xml:XML=new XML(event.target.data);
          	var app:WorkflowexApp=WorkflowexApp.createInstance();
          	var pack:Package=new Package();
          	pack.initPackage(xml);
          	
          	trace(config.getProcessURL());
        	ProcessUtil.initCavas(pack,app.getFlow(),app, app.getApplication(), app.getDrivePanel(), app.getMonitorPanel(), app.getDriveBtn(), app.getMonitorBtn(),this.config);
        	
        }
		
		/* public function resultHandler(event:ResultEvent){
			trace(httpService.headers);
			trace(event.result.toString()); */
			
			/* processXML=new XML(event.result.toString());
			
			var xml:XML=new XML(event.result.toString());
			
			xml=renderXML(xml);
			
			processTree.dataProvider=xml;
			processTree.showRoot=true;
			processTree.labelField="@name";
			processTree.labelFunction=tree_labelFunc;
			processTree.iconFunction=iconFun;
			processTree.addEventListener(ListEvent.ITEM_CLICK, tree_itemClick); */
		/* } */
		
		/* public function faultHandler(event:FaultEvent){
			trace(event.fault);
		} */
		
		/* private function httpEncoding(param:String):String{
            return encodeURIComponent(param);
        } */
        
        /* private function httpEncoding0(param:String):String{
            return param;//encodeURI(param);
        } */

	}
}